package com.kyxx.topics.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.alibaba.fastjson.JSONObject;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.R;
import com.kyxx.topics.adapter.MemberDetailAdapter;
import com.kyxx.topics.adapter.MemberPointAdapter;
import com.kyxx.topics.adapter.MemberShopAdapter;
import com.kyxx.topics.bean.MemberPromotion;
import com.kyxx.topics.net.LoadingStringCallBack;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import org.json.JSONException;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MemberPointActivity extends BaseActivity {
    @Bind(R.id.rv_member_point)
    RecyclerView rvMemberPoint;
    private String sid;
    private MemberPointAdapter adapter;
    private List<MemberPromotion> promotions;
    private final static int Notify = 1;

    protected void findViewById() {
    }

    protected void loadViewLayout() {
        setContentView(R.layout.activity_member_point);
        ButterKnife.bind(this);
        sid = getIntent().getStringExtra("shop_id");
    }


    protected void processLogic() {
        rvMemberPoint.setLayoutManager(new LinearLayoutManager(this));
        getPointInfo();
    }

    protected void setListener() {
    }

    private void getPointInfo() {
        HttpParams params = new HttpParams();
        JSONObject obj = new JSONObject();
        obj.put("sid", sid);
        params.put("json", obj.toString());
        OkGoRequest.post(UrlUtils.VIP_CARD_GET_POINT, context, "card_point" + sid, CacheMode.REQUEST_FAILED_READ_CACHE, params, new LoadingStringCallBack(MemberPointActivity.this,true) {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    org.json.JSONObject result = new org.json.JSONObject(s);
                    promotions = JSONObject.parseArray(result.getJSONArray("data").toString(), MemberPromotion.class);
                    mHandler.sendEmptyMessage(Notify);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                ToastUtils.show(context, "网络错误请稍后再试");
            }
        });
    }
    Handler mHandler = new Handler() {
        @Override
        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
            switch (msg.what) {
                case Notify:
                    adapter = new MemberPointAdapter(MemberPointActivity.this, promotions);
                    rvMemberPoint.setAdapter(adapter);
                    break;
            }
            return super.sendMessageAtTime(msg, uptimeMillis);
        }
    };

    @OnClick(R.id.iv_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
        }
    }

}